<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Controller;
use App\Models\Ad;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdController extends Controller
{
    //
    //添加编辑 广告
    public function addEditAd(Request $request){
        $id = $request->id;
        if($id){
            $res = Ad::where('id',$id)->update($request->all());
        }else{

            if(!$request->img_url)
                return $this->failed('图片必传');
            $res = Ad::create($request->all());
        }

        if($res){
            return $this->success('操作成功');
        }
        return $this->failed('操作失败');
    }


    //删除广告
    public function deleteAd(Request $request){
        $id = $request->id; //广告id
        $res = Ad::where('id',$id)->delete();
        if($res)
            return $this->success('操作成功');

        return $this->failed('操作失败');

    }


    //广告列表
    public function AdList(Request $request){

        $list = Ad::orderBy('sort')->paginate($request->pageSize?:15);

        return $this->success($list);
    }


    public function addEditAbout(Request $request){
        $id = $request->id;
        if($id){
            $res = DB::table('about')->where('id',$id)->update(['content'=>$request->about]);
        }else{

            if(!$request->about)
                return $this->failed('内容必传');
            $res = DB::table('about')->insert(['content'=>$request->about]);
        }

        if($res){
            return $this->success('操作成功');
        }
        return $this->failed('操作失败');
    }

    public function getAbout(Request $request){


        $value = DB::table('about')->first();

        return $this->success($value);
    }

}
